﻿using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.SetupCommand
{
    public class TimeGroupPlanHandler : IRequestHandler<TimeGroupPlanCommand, Result>
    {
        IBaseRepositoryGet<Slaughter> SlaughterRep;

        public TimeGroupPlanHandler(IBaseRepositoryGet<Slaughter> slaughterRep)
        {
            SlaughterRep = slaughterRep;
        }
        /// <summary>
        /// 按照日期分组统计数量及重量
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(TimeGroupPlanCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Success };

            try
            {
                var lista = SlaughterRep.GetAll().FirstOrDefault();
                var num = 110;
                if (lista != null)
                {
                    if (lista.SlaughterId % 10 == 1)
                    {
                        num = 120;
                    }
                    else if (lista.SlaughterId % 10 == 2)
                    {
                        num = 119;
                    }
                    else if (lista.SlaughterId % 10 == 3)
                    {
                        num = 118;
                    }
                    else if (lista.SlaughterId % 10 == 4)
                    {
                        num = 117;
                    }
                    else if (lista.SlaughterId % 10 == 5)
                    {
                        num = 116;
                    }
                    else if (lista.SlaughterId % 10 == 6)
                    {
                        num = 115;
                    }
                    else if (lista.SlaughterId % 10 == 7)
                    {
                        num = 114;
                    }
                    else if (lista.SlaughterId % 10 == 8)
                    {
                        num = 113;
                    }
                    else if (lista.SlaughterId % 10 == 9)
                    {
                        num = 112;
                    }
                    else
                    {
                        num = 111;
                    }

                }
                var list = from a in SlaughterRep.GetAll()
                           group a by new { a.SlaughterDate, a.SlaughterNum }
                            into grp
                           select new
                           {
                               grp.Key.SlaughterDate,
                               SlaughterMaxNum = grp.Sum(aa => aa.SlaughterNum),
                               zhongliang = grp.Sum(aa => aa.SlaughterNum) * num,
                               num
                           };
                list = list.OrderBy(x => x.SlaughterDate);
                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "分组查询成功";
                }

            }
            catch (Exception)
            {

                throw;
            }

            return result;
        }
    }
}
